/**
 * Copyright (c) 2009 blurredVision
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
 * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#ifndef __DX_TEXTURE_H__
#define __DX_TEXTURE_H__

#pragma once

#include <d3dx9.h>
#include <string>

#include "ITexture.h"
#include "DXResource.h"

class IShaderEffect;

namespace directx {

/**
 *  The DirectX implementation of the texture object
 */
class Texture 
	: public ITexture, public DXResource
{
public:
	/** Loads an image from a file for use as a texture */
	Texture();
	virtual ~Texture(void);

	void reset();

	// Release the texture resources
	void release();

	/** Binds the texture object for use in the rendering pass */
	bool bind(unsigned int uiTexTarget = 0);

	/** Unbinds the texture object for use in the rendering pass */
	bool unbind();

	/** Loads a texture from a file */
	bool load(const std::string &filename, bool dynamic = false, eFormat format = DEFAULT);

	/** Creates a render target from an empty texture. 
	 *  The width and height of the texture must be specified */
	bool createRenderTarget(int width, int height, eFormat format, bool useDepth = true);

	bool createRenderToTexture(int width, int height, eFormat format, bool usedepth = true);

	bool createFromData(int width, int height, eFormat format, char* data);
	bool beginScene();
	bool endScene();

	/** Display a texture on screen */
	void display(int offsetX, int offsetY, float scaleFactor, bool bIgnoreBlend, const math::Vector4 &color, bool enableFilter);
	
	unsigned char* getPixel(unsigned int x, unsigned int y);

// DirectX specific
public:
	bool computeNormalMap( const std::string &filename );
	IDirect3DSurface9*       getRenderTarget() const;
	DLL_EXPORT IDirect3DTexture9*       getD3DTexture() const;
	IDirect3DVolumeTexture9* getD3DVolumeTexture() const;
	IDirect3DSurface9*       getRenderTargetDepthStencil() const;

	const unsigned int sizeOfFormat(D3DFORMAT) const;

	static D3DFORMAT convertFormatToD3D(eFormat format);
	static eFormat   convertFormatFromD3D(D3DFORMAT format);

private:
	LPDIRECT3DTEXTURE9       m_pTexture;
	LPDIRECT3DVOLUMETEXTURE9 m_pVolTexture;
	LPDIRECT3DSURFACE9       m_pRenderTargetSurface;
	LPDIRECT3DSURFACE9       m_pRenderTargetDepthStencilSurface;
	
	LPD3DXRENDERTOSURFACE    m_pRTS;
	D3DVIEWPORT9			 m_sViewport;

	LPDIRECT3DTEXTURE9       m_pTextureLockable;
	LPDIRECT3DSURFACE9       m_pSurfaceLockable;

	D3DLOCKED_RECT lr;
	D3DSURFACE_DESC desc;
	unsigned char*  pPixelAddress;

	unsigned int hParameters;

	IShaderEffect *pEffect;
};

} // namespace directx

#endif
